package com.visionork.android.twe.activity;

import java.util.ArrayList;
import java.util.List;

import com.visionork.android.twe.RepositoryKey;
import com.visionork.android.twe.TWEApplication;
import com.visionork.android.twe.model.Episode;
import com.visionork.android.twe.model.Part;
import com.visionork.android.twe.model.RepositoryBase;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class EpisodeActivity extends ListActivity {
	public static final String PROGRAMKEY = "PROGRAMKEY";
	public static final String KEY = "KEY";
	public static final String REPOSITORY = "REPOSITORY";
	public static final String DYNAMIC = "DYNAMIC";
	public static final String URLSTRING = "URLSTRING";
	private ArrayList<Part> parts;
	private String programKey;
	private String key;
	private int repositoryKey;
	private boolean dynamic;
	private String urlString;

	private class LoadYoutubeTask extends AsyncTask<Integer, Void, Void> {
		protected ProgressDialog pd;
		protected String youtubeID;

		@Override
		protected Void doInBackground(Integer... params) {
			try {
				int position = params[0];
				Part part = null;
				if (!dynamic) {
					part = getRepository().getProgram(programKey)
							.getEpisode(key).getParts().get(position);
				} else {
					part = parts.get(position);
				}
				youtubeID = part.getYoutubeID();

			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			Resources res = getResources();
			pd = ProgressDialog.show(EpisodeActivity.this,
					res.getString(R.string.d_loading),
					res.getString(R.string.d_loading_detail), true, false);
		}

		@Override
		protected void onPostExecute(Void param) {
			pd.dismiss();
			Intent intent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("vnd.youtube:" + youtubeID));
			startActivity(intent);
		}
	}

	private class LoadPartTask extends AsyncTask<Void, Void, Void> {
		protected ProgressDialog pd;

		@Override
		protected Void doInBackground(Void... params) {
			try {
				if (!dynamic) {
					parts = getRepository().getProgram(programKey)
							.getEpisode(key).getParts();
				} else {
					parts = Episode.CrawlHelper(urlString);
				}
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			Resources res = getResources();
			pd = ProgressDialog.show(EpisodeActivity.this,
					res.getString(R.string.d_loading),
					res.getString(R.string.d_loading_detail), true, false);
		}

		@Override
		protected void onPostExecute(Void param) {
			List<String> titles = new ArrayList<String>(parts.size());

			for (Part part : parts) {
				titles.add(part.getTitle());
			}

			setListAdapter(new ArrayAdapter<String>(EpisodeActivity.this,
					R.layout.part_list_item, titles));

			pd.dismiss();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.part_list);
		programKey = getIntent().getStringExtra(PROGRAMKEY);
		key = getIntent().getStringExtra(KEY);
		repositoryKey = getIntent().getIntExtra(REPOSITORY,
				RepositoryKey.ShowRepository);
		dynamic = getIntent().getBooleanExtra(DYNAMIC, false);
		urlString = getIntent().getStringExtra(URLSTRING);
		loadPart();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		LoadYoutubeTask task = new LoadYoutubeTask();
		task.execute(new Integer[] { position });
	}

	private void loadPart() {
		LoadPartTask task = new LoadPartTask();
		task.execute(new Void[] {});
	}

	protected RepositoryBase getRepository() {
		return TWEApplication.getRepository(repositoryKey);
	}
}
